const {
	exec,
	escape
} = require('../mysql.js')
const {
	formatTime
} = require('../../utils/time.js')
// 登录查询
function login(userName, passWord) {
	userName = escape(userName)
	passWord = escape(passWord)
	let sql = `select id from user where user_name=${userName} and pass_word=${passWord}`
	return exec(sql)
}

function addUser(userName, passWord, name, phone,email,userimg) {
	if(userimg == '' || !userimg){
		userimg = 'https://img2.baidu.com/it/u=2247434761,1740252655&fm=26&fmt=auto&gp=0.jpg'
	}
	userName = escape(userName)
	passWord = escape(passWord)
	name = escape(name)
	phone = escape(phone)
	email = escape(email)
	userimg= escape(userimg)
	let time = new Date()
	let addTime = formatTime(time)
	addTime = escape(addTime)
	
	let sql = `insert into user (user_name,pass_word,create_time,name,phone,email,user_img) values (${userName},${passWord},${addTime},${name},${phone},${email},${userimg});`;
	console.log('sql',sql);
	return exec(sql)
}

// 查询相同name
function queryName(userName){
	userName = escape(userName)
	console.log('name',userName);
	let sql = `select user_name from user where user_name=${userName}`
	return exec(sql)
}


function getUserInfo(username){
	username = escape(username)
	let sql = `select id,name,user_name,user_img,email,phone from user where user_name=${username}`
	return exec(sql)
}
module.exports = {
	login,
	addUser,
	queryName,
	getUserInfo
}
